package com.pubinfo.passbook.common.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.mychain.gov.sdk.base.TrustedInvoiceContent;
import com.alipay.mychain.gov.sdk.base.model.BaseInvoiceContent;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.collections4.CollectionUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * 税务发票实体类
 *
 * @TableName t_source_invoice
 */
@Data
@NoArgsConstructor
@TableName(value = "t_source_invoice", autoResultMap = true)
@ApiModel(value = "TSourceInvoice", description = "发票数据")
public class TSourceInvoice extends Model implements Serializable {
  
  //@TableField(exist = false)
  public static final long serialVersionUID = 1L;
  /**
   *
   */

  @TableId(type = IdType.ASSIGN_UUID, value = "id")
  @ApiModelProperty("发票id")
  public String id;
  
  /**
   * 发票类型
   */
  @ApiModelProperty("发票类型")
  public String fplx;
  @ApiModelProperty("公司主体名称")
  public String nsrsbhName;
  
  /**
   * 发票代码
   */
  @ApiModelProperty("发票类型")
  public String fpdm;
  
  /**
   * 发票号码
   */
  @ApiModelProperty("发票号码")
  public String fphm;
  
  /**
   * 开票⽇期
   */
  @ApiModelProperty("开票⽇期")
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  public Date kprq;
  
  @ApiModelProperty("加工⽇期")
  @JsonFormat(pattern = "yyyy-MM-dd")
  public Date jgrq;
  /**
   * 购⽅识别号
   */
  @ApiModelProperty("购⽅识别号")
  public String gfsbh;
  
  /**
   * 销⽅识别号
   */
  @ApiModelProperty("销⽅识别号")
  public String xfsbh;
  
  /**
   * 发票状态代码
   */
  @ApiModelProperty("发票状态代码：0正常，1 失控，2作废，3红字，4 异常票")
  public String fpzt_dm;
  
  /**
   * 加密后原始数据
   */
//  @ApiModelProperty("加密后原始数据")
//  public String content;
  
  /**
   * 发票认证相关数据
   */
  @ApiModelProperty("发票认证相关数据")
  public String trustedData;
  
  @TableField(typeHandler = FastjsonTypeHandler.class)
  public JSONObject dataJson;
  /**
   * 认证数据,是否为真 0 为真 1为假
   */

  @ApiModelProperty("认证数据,是否为真 0 为真 1为假")
  public Integer status;
  /**
   * 创建时间
   */
  @ApiModelProperty("创建时间")
  @TableField(fill = FieldFill.INSERT)
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  public Date createTime;
  
  @ApiModelProperty(value = "同步任务日志id")
  private String logId;
  
  @ApiModelProperty("金额")
  private BigDecimal je = BigDecimal.ZERO;
  @ApiModelProperty("税额")
  private BigDecimal se = BigDecimal.ZERO;
  @ApiModelProperty("价税合计")
  private BigDecimal jshj = BigDecimal.ZERO;
  @ApiModelProperty("修改时间")
  @TableField(fill = FieldFill.UPDATE)
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  public Date updateTime;
  
  public TSourceInvoice(BaseInvoiceContent baseInvoiceContent, TrustedInvoiceContent content) {
    this.fpdm = baseInvoiceContent.getFPDM();
    this.fphm = baseInvoiceContent.getFPHM();
    this.fplx = baseInvoiceContent.getFPLX();
    this.fpzt_dm = baseInvoiceContent.getFPZT_DM();
    this.gfsbh = baseInvoiceContent.getGFSBH();
    this.xfsbh = baseInvoiceContent.getXFSBH();
    this.kprq = baseInvoiceContent.getKPRQ();
    //  String contentStr = new String(content.getInvoiceContent());
    // this.content = Base64.getEncoder().encodeToString(content.getInvoiceContent());
    String jsonStr = new String(content.getInvoiceContent(), StandardCharsets.UTF_8);
    JSONObject jsonObject = JSON.parseObject(jsonStr);
    
    this.dataJson = jsonObject;
    // 暂时不保存 this.trustedData = content.getTrustedData();
  }
  
  @SneakyThrows
  public static Integer toInteger(List<Future<Integer>> futureList) {
    if (CollectionUtils.isEmpty(futureList)) {
      return null;
    }
    Integer count = 0;
    for (Future<Integer> integerFuture : futureList) {
      count += integerFuture.get();
    }
    return count;
  }
  
  public static List<TSourceInvoice> toList(List<Future<List<TSourceInvoice>>> futureList) {
    if (CollectionUtils.isEmpty(futureList)) {
      return null;
    }
    List<TSourceInvoice> sourceInvoices = new ArrayList<>();
    futureList.stream().forEach(future -> {
      try {
        sourceInvoices.addAll(future.get());
      } catch (InterruptedException e) {
        e.printStackTrace();
      } catch (ExecutionException e) {
        e.printStackTrace();
      }
    });
    return sourceInvoices;
  }

  public int getFpdmAndFphmHash()
  {
    return Objects.hash(this.fpdm,this.fphm);
  }
//  public String getContent(){
//    return new String(Base64.getDecoder().decode(this.content));
//  }
}